package fr.pud.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 * This class provides some useful methods to deal with XML files
 * @author Pierre Reliquet
 */
public class Tools4XML {
    /**
     * @param o
     *            , an object which is supposed to be written in a xml file
     * @param nameOfClass
     *            , the name of the class which is the real type of object o
     *            (for example Round.class)
     * @param nameOfFile
     *            , the name of the file which is going to be created
     */
    @SuppressWarnings("rawtypes")
    public static void object2XML(Object o, Class nameOfClass, String nameOfFile) {
        try {
            String path = Tools.DATA_FILE_FOLDER.getAbsolutePath()
                    + File.separatorChar + nameOfFile + ".xml";
            FileWriter file = new FileWriter(path);
            try {
                JAXBContext context = JAXBContext.newInstance(nameOfClass);
                Marshaller m = context.createMarshaller();
                m.marshal(o, file);
            }
            catch (JAXBException e) {
                JOptionPane.showMessageDialog(null,
                        "<html>An error occured while generating the XML code!<br />Description: "
                                + e.getStackTrace() + "</html>", "Error",
                        JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
            finally {
                file.close();
            }
        }
        catch (IOException e) {
            Tools.writeToLogFile("An error occured while generating the file!",
                    Tools.LOG_FILE_SHOW_JOPTION_PANE, e.getStackTrace());
        }
    }
    /**
     * @param path
     *            , the path of the file which is going to be import
     * @param c
     *            , the type of the object which is represented in the xml file
     * @return a String which contains the data of the xml file and which is
     *         formatted with html
     */
    // @SuppressWarnings("rawtypes")
    // public static String xml2JLabelString(String path, Class c) {
    // ArrayList<String> data = new ArrayList<String>();
    // try {
    // JAXBContext context = JAXBContext.newInstance(c);
    // Unmarshaller um = context.createUnmarshaller();
    // if (c.toString().equals(ROUND)) {
    // PUDRound r = (PUDRound) um.unmarshal(new File(path));
    // ArrayList<PUDStation> list = (ArrayList<PUDStation>) r
    // .getStations();
    // String line = "";
    // for (int i = 0; i < list.size(); i++) {
    // line += (i + 1) + ": "
    // + list.get(i).getAddress().getAddressName();
    // line += ", "
    // + LanguagesConstants.LANGUAGE
    // .getString(LanguagesConstants.ADDRESS)
    // + ": " + list.get(i).getAddress();
    // line += ", "
    // + LanguagesConstants.LANGUAGE
    // .getString(LanguagesConstants.STATION_OPENING)
    // + ": " + list.get(i).getOpening();
    // line += ", "
    // + LanguagesConstants.LANGUAGE
    // .getString(LanguagesConstants.STATION_CLOSING)
    // + ": " + list.get(i).getClosing() + "<br />";
    // }
    // data.add(line);
    // } else {
    // Fleet f = (Fleet) um.unmarshal(new File(path));
    // ArrayList<Vehicle> list = (ArrayList<Vehicle>) f.getVehicles();
    // String line = "";
    // for (int i = 0; i < list.size(); i++) {
    // line += LanguagesConstants.LANGUAGE
    // .getString(LanguagesConstants.VEHICLE)
    // + (i + 1)
    // + ": ";
    // line += ", "
    // + LanguagesConstants.LANGUAGE
    // .getString(LanguagesConstants.VEHICLE_CAPACITY)
    // + " : " + list.get(i).getCapacity();
    // line += ",  "
    // + LanguagesConstants.LANGUAGE
    // .getString(LanguagesConstants.VEHICLE_AUTONOMY)
    // + " : " + list.get(i).getAutonomy();
    // line += ",  "
    // + LanguagesConstants.LANGUAGE
    // .getString(LanguagesConstants.VEHICLE_STARTING_POINT_NAME)
    // + "  : " + list.get(i).getStartingPoint();
    // line += ",  "
    // + LanguagesConstants.LANGUAGE
    // .getString(LanguagesConstants.VEHICLE_ENDING_POINT_NAME)
    // + "  : " + list.get(i).getEndingPoint() + "<br />";
    // }
    // data.add(line);
    // }
    // } catch (JAXBException e) {
    // Tools.writeToLogFile(
    // "The selected file is not a compatible XML file",
    // Tools.LOG_FILE_SHOW_JOPTION_PANE, e.getStackTrace());
    // return null;
    // }
    // String label = "<html>";
    // for (int i = 0; i < data.size(); i++) {
    // label += data.get(i);
    // }
    // label = label.concat("</html>");
    // return label;
    // }
    // public static void main(String[] args) {
    // File f = new File("xml/example1.xml");
    // System.out.println(f.getAbsolutePath());
    // parseResultFile(f.getAbsolutePath());
    // // ArrayList<Coordinate> l = parseXMLFile(f);
    // // System.out.println(l.size());
    // // for (int i = 0; i < l.size(); i++) {
    // // System.out.println(l.get(i));
    // // }
    // }
}